//
//  main.m
//  07-HomeWork
//
//  Created by dllo on 16/7/12.
//  Copyright © 2016年 daming. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Zone.h"
#import "TempCity.h"
#import "Province.h"
#import "City.h"
int main(int argc, const char * argv[]) {
#pragma mark - area
    NSString *path = @"/Users/dllo/Desktop/Province-City-Zone/Province-City-Zone/area.txt";
    // 取到字符串
    NSString *sourceString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    // 取出(根据"\n")截取的所有的字符串, 放入数组中
    NSArray *allArray = [sourceString componentsSeparatedByString:@"\n"];
    NSMutableArray *allProArray = [NSMutableArray array];
    for (NSString *area in allArray) {
        if (![area hasPrefix:@" "]) {
            Province *pro = [Province zoneWithName:area];
            [allProArray addObject:pro];
        } else if ([area hasPrefix:@"  "] && ![area hasPrefix:@"    "]) {
            Province *pro = [allProArray lastObject];
            City *city = [City zoneWithName:area];
            [pro.array addObject:city];
        } else {
            City *city = [[[allProArray lastObject] array] lastObject];
            Zone *zone = [Zone zoneWithName:area];
            [city.array addObject:zone];
        }
    }
    NSLog(@"%@", allProArray);
    
#pragma mark - Location1
    NSString *locationPath = @"/Users/dllo/Desktop/Province-City-Zone/Province-City-Zone/Location1.txt";
    NSString *source = [NSString stringWithContentsOfFile:locationPath encoding:NSUTF8StringEncoding error:nil];
    // 根据","分割, 得到所有城市的数组
    NSArray *allCityArray = [source componentsSeparatedByString:@","];
    NSMutableArray *allTempCityArray = [NSMutableArray array];
    for (NSString *cityString in allCityArray) {
        // 根据"|" 切割字符串
        NSArray *city = [cityString componentsSeparatedByString:@"|"];
        TempCity *tempCity = [[TempCity alloc] initWithCityName:[city firstObject] cityNumber:[city lastObject]];
        [allTempCityArray addObject:tempCity];
        // 对内存进行管理
        [tempCity release];
    }
    // 对类数组进行排序
    NSSortDescriptor *sortTempCity = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
    [allTempCityArray sortUsingDescriptors:@[sortTempCity]];
    // 进行内存管理
    [sortTempCity release];
    NSMutableArray *finalClassArray = [NSMutableArray array];
    for (TempCity *tempCity in allTempCityArray) {
        if ([tempCity.cityNumber hasSuffix:@"0000"]) {
            Province *pro = [Province zoneWithTempCity:tempCity];
            [finalClassArray addObject:pro];
        } else if ([tempCity.cityNumber hasSuffix:@"00"] && ![tempCity.cityNumber hasSuffix:@"0000"]) {
            Province *pro = [finalClassArray lastObject];
            City *city = [City zoneWithTempCity:tempCity];
            [pro.array addObject:city];
        } else {
            Province *pro = [finalClassArray lastObject];
            City *city = [pro.array lastObject];
            Zone *zone = [Zone zoneWithTempCity:tempCity];
            [city.array addObject:zone];
        }
    }
    
    //    NSLog(@"%@", finalClassArray);

    
    return 0;
}
